<?php

class ArtistsController extends Controller
{
	public function actionIndex()
	{
		$model=new Artists('search');
		$model->unsetAttributes();  // clear any default values
		if(isset($_GET['Artists']))
			$model->attributes=$_GET['Artists'];

		$this->render('index',array(
			'model'=>$model,
		));
	}

	public function actionCreate(){
		$model = new Artists;
		$profile = new ArtistProfile ;
		$social = new ArtistSocial ;
		if (isset($_POST['Artists'],$_POST['ArtistProfile'],$_POST['ArtistSocial'])) {
			$model->attributes = $_POST['Artists'];		
			$profile->attributes = $_POST['ArtistProfile']; 

			if(CUploadedFile::getInstance($model,'image') != null){
				$model->image = CUploadedFile::getInstance($model,'image');
		        $image = $this->createSlug($_POST['Artists']['fullname']) .'-'. time() . '.' . $model->image->getExtensionName();
		        $model->image->saveAs(IMAGES_DIR.'/Artists/' . $image);
		        $model->image = $image;
		        $model->url = $this->createSlug($model->fullname);
		        $model->createddate = date("d-m-Y H:i:s");
		        if($model->validate()){ 
		        	$model->save(false);

					$profile->artist_id = $model->id;
					$profile->description = $_POST['ArtistProfile']['description'];
					$profile->save(); 

					foreach($_POST['ArtistSocial']['social_id'] as $k=>$data)
                    {
		                    $addsocial = new ArtistSocial;  
		                    $addsocial->social_id = $_POST["ArtistSocial"]['social_id'][$k];
		                    $addsocial->url = $_POST["ArtistSocial"]['url'][$k];
		                    $addsocial->artist_id = $model->id;
		                    $addsocial->save() ;
                	}
					$this->redirect(array('view','id'=>$model->id));
				}
			}else{
		        Yii::app()->user->setFlash('error',"<b>Bạn chưa chọn hình ảnh.<b/>");
		     }

		}
		$this->render('create',array(
			'model'=>$model,'profile'=>$profile,'social'=>$social,
		));
	}

	public function actionUpdate($id)
	{
		$model= Artists::model()->findByPk($id);
		$old_image = $model->image ;
		$social =ArtistSocial::model()->findAllByAttributes(array('artist_id'=>$id));
		if(empty($social)){
			$social = new ArtistSocial ;
		}
		$profile =ArtistProfile::model()->findByAttributes(array('artist_id'=>$id));
		if(empty($profile)){
			$profile = new ArtistProfile;
		}

		if(isset($_POST['Artists']))
		{
				$model->attributes=$_POST['Artists'];
				$profile->attributes = $_POST['ArtistProfile']; 
				if(CUploadedFile::getInstance($model,'image') != null){
					$model->image = CUploadedFile::getInstance($model,'image');
			        $image = $this->createSlug($_POST['Artists']['fullname']) .'-'. time() . '.' . $model->image->getExtensionName();
			        $model->image->saveAs(IMAGES_DIR.'/Artists/' . $image);
			        $model->image = $image;
			    }else{
			    	$model->image = $old_image ;
			    }
					if($model->validate()){ 
			        	$model->save(false);

						$profile->artist_id = $model->id;
						$profile->description = $_POST['ArtistProfile']['description'];
						$profile->save(); 

						foreach($_POST['ArtistSocial']['social_id'] as $k=>$data)
                    	{
		                    $addsocial = new ArtistSocial;  
		                    $addsocial->social_id = $_POST["ArtistSocial"]['social_id'][$k];
		                    $addsocial->url = $_POST["ArtistSocial"]['url'][$k];
		                    $addsocial->artist_id = $model->id;
		                    $addsocial->save() ;
                		}

						$this->redirect(array('view','id'=>$model->id));
				}
		  }

		$this->render('update',array(
			'model'=>$model,'profile'=>$profile,'social'=>$social,
		));
	}

	// add image to ArtistImages
	public function actionAddImage($id){
		$model = Artists::model()->findByPk($id);
		$listImage = ArtistImages::model()->findAllByAttributes(array('artist_id'=>$id));
		$artistImages = new ArtistImages ;
		if(isset($_FILES['image']))
        {
            $images = CUploadedFile::getInstancesByName('image');
            if (isset($images) && count($images) > 0) {
                foreach ($images as $key => $pic) {
                	$fileName = 'artists_' .$pic->name. time(). '.' .$this->getExtensionName($pic->name) ;
                    if ($pic->saveAs(IMAGES_DIR.'/Artists/' .$fileName)) {
                        $artistImages = new ArtistImages() ;
                        $artistImages->image = $fileName;
                        $artistImages->artist_id = $model->id;
                        $artistImages->save(); // DONE
                    }
                    else{
                    	Yii::app()->user->setFlash('error',"<b>error.<b/>");
                    }
                }
            }
        }
		$this->render('addImage',array('model'=>$model,'image'=>$artistImages,'listImage'=>$listImage));
	}

	public function getExtensionName($name)
	{
	    if(($pos=strrpos($name,'.'))!==false)
	        return (string)substr($name,$pos+1);
	    else
	        return '';
	}

	public function actionView($id){
		if(isset($id)){
			$model = Artists::model()->findByPk($id);
			$profile = ArtistProfile::model()->findByAttributes(array('artist_id'=>$id));
			$social = ArtistSocial::model()->findAllByAttributes(array('artist_id'=>$id));
			$image = ArtistImages::model()->findAllByAttributes(array('artist_id'=>$id));
			$this->render('view',array('model'=>$model,'profile'=>$profile,'social'=>$social,'image'=>$image));
		}
		
	}

	public function actionDelete($id)
	{
		$this->loadModel($id)->delete();
        $criteria = new CDbCriteria;
        $criteria->condition='artist_id='.$id;
        ArtistProfile::model()->deleteAll($criteria);
        ArtistSocial::model()->deleteAll($criteria);
		// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
		if(!isset($_GET['ajax']))
			$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('index'));
	}
}